package com.example.thecharacterbook;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements TheCheck_MYSELF.CHECKMESELF {
    @Override
    public void ChangeVar(String Word, String ID, String Mean, String E1, String E2, String E3) {

    }

    private FloatingActionButton AddBtn;
    private MyDataBase db;
    ArrayList<String> word_id, word, mean, example, example2, example3;
    MyAdapter myAdapter;
    RecyclerView recyclerView;
    private ImageButton SearchInternet,Map;
    private int i = 0;
    Activity ORIENTATION_LANDSCAPE;
    Activity ORIENTATION_PORTRAIT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Trriger!", "-----------");
        int orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.activity_main);
            AddBtn = findViewById(R.id.Add);
            recyclerView = findViewById(R.id.Rv);
            AddBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MainActivity.this, TheAddMethod.class);
                    startActivityForResult(intent, 1);
                }
            });
            db = new MyDataBase(this);
            word_id = new ArrayList<>();
            word = new ArrayList<>();
            mean = new ArrayList<>();
            example = new ArrayList<>();
            example2 = new ArrayList<>();
            example3 = new ArrayList<>();
            Map = findViewById(R.id.search_map);
            SearchInternet =findViewById(R.id.search_page);
            Map.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent a = new Intent(MainActivity.this,MainActivity3.class);
                    startActivity(a);
                }
            });
            SearchInternet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent a = new Intent(MainActivity.this,MainActivity2.class);
                    startActivity(a);
                }
            });
            Log.v("word_id", "ok");
            Log.v("word_id", "IS" + word_id);
            displayData();
            myAdapter = new MyAdapter(MainActivity.this, MainActivity.this, word_id, word, mean, example, example2, example3);
            Log.v("word_id", "IS" + word_id);
            Log.v("word", "IS" + word);
            recyclerView.setAdapter(myAdapter);
            DividerItemDecoration dI = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
            recyclerView.addItemDecoration(dI);
            recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.fragment_the_check);
            TheCheck a = new TheCheck();
            TheCheck_MYSELF check_myself = new TheCheck_MYSELF();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.Rv_theScreen, a);
            transaction.add(R.id.container_theScreen, check_myself);
            transaction.commitAllowingStateLoss();

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            recreate();
        }
    }

    void DeleteData() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search, menu);
        MenuItem item = menu.findItem(R.id.Menu_Search1);
        SearchView searchView = (SearchView) item.getActionView();
        if (searchView.equals(null)) {

        }
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                myAdapter.getFilter().filter(newText);
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    void displayData() {
        Cursor cursor = db.readAllData();
        System.out.println(cursor);
        if (cursor.getCount() == 0) {
            Toast.makeText(this, "Input Error!", Toast.LENGTH_SHORT).show();
        } else {
            while (cursor.moveToNext()) {
                word_id.add(cursor.getString(0));
                word.add(cursor.getString(1));
                mean.add(cursor.getString(2));
                example.add(cursor.getString(3));
                example2.add(cursor.getString(4));
                example3.add(cursor.getString(5));
            }
        }
    }
}